package com.zxl.job;


import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.zxl.entities.MmMessage;
import com.zxl.handle.MmMessageHandler;
import com.zxl.service.MmMessageService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;


@JobHandler(value = "consumeMsgJobHandler")
@Component
public class ConsumeMsgJobHandler extends IJobHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumeMsgJobHandler.class);
    @Resource
    private MmMessageHandler msgHandler;

    @Resource
    private MmMessageService msgService;

    //过期时间 单位分钟
    private static final int VERDUE_TIME = 1;

    @Override
    public ReturnT<String> execute(String param) throws Exception {
        LOGGER.info("进入消费定时任务..........................");
        this.processMsg();
        return SUCCESS;
    }

    public void processMsg() {
        //1.消息服务定期轮询，过期的“已发送”消息（过期一般是根据业务规则来自行调整，例如2min）
        List<MmMessage> msgList = this.msgService.selectOverdueMsgBySend(VERDUE_TIME);
        for (MmMessage msg : msgList) {
            //2.消息服务重新将过期的“已发送”消息，发送到消息中间件MQ中。
            msgHandler.send(msg.getRoutingKey(), msg.getJsonMsg());
        }
    }
}
